function checkStatus(value) {
    var status = value.charAt(0);
    
//    define("OK_START_GAME" , "0");
//    define("WAIT_TO_START_GAME" , "1");
//    define("ERROR_WAITING" , "3");
    
    switch (status) {
        case "1":
            // similar behavior as an HTTP redirect
            window.location.replace("game.php");            
            break;
        case "2":
            // Still Waiting...
            //document.getElementById("WaitLable").innerHTML = "Waiting for opponent...";
            break;
        case "3":
            document.getElementById("WaitLable").innerHTML = "Server Error:" + value;
            break;            
    }
    

}

function getStatus() {
  if (window.XMLHttpRequest) { // Mozilla, Chrome, ...
    http_request = new XMLHttpRequest();
  } 
  else if (window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        http_request = new 
                       ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
 
  if (!http_request) {
    alert("Cannot generate request object.");
    return;
  }

  http_request.onreadystatechange = function() {
    if (http_request.readyState < 4) {
      // ... loading ...
      return;
    }
    if (http_request.status == 200) {
      checkStatus(http_request.responseText);
    } 
    else {
      alert("Failed to get response from server, code: " + http_request.status);
      return;
    }
  };
  http_request.open('POST', "waiting_state.php", true);
  http_request.send(null);
}

setInterval(getStatus, 3000);